package com.guigu.wenzhiyi.controller;

import com.github.pagehelper.PageInfo;
import com.guigu.tankai.pojo.Member;
import com.guigu.wenzhiyi.pojo.Interposefollowupvisit;
import com.guigu.wenzhiyi.service.InterposefollowupvisitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("Interposefollowupvisit")
public class InterposefollowupvisitController {

    @Autowired
    InterposefollowupvisitService interposefollowupvisitService;

    @RequestMapping("page")
    public PageInfo<Interposefollowupvisit> page(Integer num, Integer size, String name) {
        return interposefollowupvisitService.page(num, size, name);
    }

    @RequestMapping("MemCha")
    public List<Member> MemCha(String name) {
        return interposefollowupvisitService.cha(name);
    }
    @RequestMapping("File")
    public void File(@RequestParam("pictureFile") MultipartFile pictureFile, HttpSession session) throws IOException {
        // 图片上传
        // 设置图片名称，不能重复，可以使用uuid
        String picName = UUID.randomUUID().toString();
        // 获取文件名
        String oriName = pictureFile.getOriginalFilename();
        // 获取图片后缀
        String extName = oriName.substring(oriName.lastIndexOf("."));
        File file=new File("C:\\Users\\a\\Desktop\\项目\\vue-element-ui--demo\\Rear\\html\\src/image");
        if (!file.exists()) {
            file.mkdirs();
        }
        // 开始上传
        pictureFile.transferTo(new File(file + "/" + picName + extName));
        session.setAttribute("pictureFile", picName + extName);
    }

    @RequestMapping("add")
    public int File(@RequestBody Interposefollowupvisit interposefollowupvisit, HttpSession session) throws IOException {
        String pictureFile = (String) session.getAttribute("pictureFile");
        if (pictureFile != null) {
            interposefollowupvisit.setAttachmentfile(pictureFile);
        }
        return interposefollowupvisitService.add(interposefollowupvisit);
    }

    @RequestMapping("update")
    public int update(@RequestBody Interposefollowupvisit interposefollowupvisit, HttpSession session) throws IOException {
        String pictureFile = (String) session.getAttribute("pictureFile");
        if (pictureFile != null) {
            interposefollowupvisit.setAttachmentfile(pictureFile);
        }
        return interposefollowupvisitService.update(interposefollowupvisit);
    }

    @RequestMapping("delete")
    public int delete(Integer id) {
        return interposefollowupvisitService.delete(id);
    }


}
